﻿Imports MySql.Data.MySqlClient
Public Class personal3_1
    Public SQL As MySqlConnection
    Public code As String
    Public names As String
    Public coursename As String
    Public courseid As String
    Public checkdata As Boolean
    Public checkstring As String
    Public key As String
    Public Sub New(ByRef SQLConnection As MySqlConnection)
        InitializeComponent()
        SQL = SQLConnection
    End Sub

    ' Private Sub personal3_1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    'Dim mainForm As main = New main()
    '   mainForm.Show()
    'Me.Close()
    'End Sub

    Private Sub personal3_1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        checkdata = False
        showdata()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        showdata()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If checkdata = False Or checkstring = "" Then
            MsgBox("Please select your data", MsgBoxStyle.Critical, "Warning Message")
        Else
            code = ListView1.SelectedItems(0).Text
            If ListView1.SelectedItems(0).SubItems(1).Text = "-" Then
                names = ListView1.SelectedItems(0).SubItems(2).Text
            ElseIf ListView1.SelectedItems(0).SubItems(1).Text <> "-" Then
                names = ListView1.SelectedItems(0).SubItems(1).Text + ListView1.SelectedItems(0).SubItems(2).Text
            End If
            coursename = ""
            Dim mainForm As txt_totalCost = New txt_totalCost(SQL, names, code, courseid, coursename)
            mainForm.Show()
            Me.Close()
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim mainForm As main = New main()
        mainForm.Show()
        Me.Close()
    End Sub

    Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
        checkstring = ListView1.SelectedItems(0).Text
    End Sub

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = "13" Then
            showdata()
        End If
    End Sub

    Private Sub showdata()
        Dim mySqlCommand As New MySqlCommand
        Dim mySqlAdaptor As New MySqlDataAdapter
        Dim mySqlReader As MySqlDataReader

        If SQL.State = ConnectionState.Closed Then
            SQL.Open()
        End If

        key = TextBox1.Text
        'MsgBox(key)

        '        If TextBox1.Text = "" Then
        'MsgBox("Please Enter Your Keywords", MsgBoxStyle.Critical, "Warning Message")
        'Else
        Try
            mySqlCommand.CommandText = "SELECT * FROM  Employee where   (Employee.emp_name like " + "'%" + TextBox1.Text + "%' or Employee.emp_surname like " + "'%" + TextBox1.Text + "%' or Employee.emp_code like " + "'%" + TextBox1.Text + "%' " + "or Employee.emp_gender like " + "'%" + TextBox1.Text + "%'" + "or Employee.emp_position like " + "'%" + TextBox1.Text + "%'" + "or Employee.emp_level like " + "'%" + TextBox1.Text + "%') and emp_status = '1' ORDER BY Employee.emp_name ;"
            mySqlCommand.Connection = SQL
            mySqlAdaptor.SelectCommand = mySqlCommand
            mySqlReader = mySqlCommand.ExecuteReader

            ListView1.Items.Clear()
            While (mySqlReader.Read())
                With ListView1.Items.Add(mySqlReader("emp_code"))
                    Try
                        .subItems.add(mySqlReader("emp_gender"))
                    Catch
                        .subItems.add("-")
                    End Try
                    .subItems.add(mySqlReader("emp_name") + " " + mySqlReader("emp_surname"))
                    .subItems.add(mySqlReader("emp_position"))
                    .subItems.add(mySqlReader("emp_level"))
                End With
            End While
            checkdata = True
            mySqlReader.Close()

            SQL.Close()
        Catch

        End Try

    End Sub

    Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
        If checkdata = False Or checkstring = "" Then
            MsgBox("Please select your data", MsgBoxStyle.Critical, "Warning Message")
        Else
            code = ListView1.SelectedItems(0).Text

            If ListView1.SelectedItems(0).SubItems(1).Text = "-" Then
                names = ListView1.SelectedItems(0).SubItems(2).Text
            ElseIf ListView1.SelectedItems(0).SubItems(1).Text <> "-" Then
                names = ListView1.SelectedItems(0).SubItems(1).Text + ListView1.SelectedItems(0).SubItems(2).Text
            End If


            coursename = ""
            Dim mainForm As txt_totalCost = New txt_totalCost(SQL, names, code, courseid, coursename)
            mainForm.Show()
            Me.Close()
        End If
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class